<template>
  <div class="app-container  bg-gray">
    <transition name="fade-transform" mode="out-in">
      <router-view />
    </transition>
    <el-row v-if="isShow" class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-select v-model="searchTerms.job_status" placeholder="在职 | 离职" class="select" clearable @change="getData">
                <el-option label="在职" value="1" />
                <el-option label="离职" value="0" />
              </el-select>
              <el-select v-model="searchTerms.company_id" placeholder="部门" class="select" clearable @change="getData">
                <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
            <div>
              <!--<el-button v-if="queryMatch(114)" type="success" class="bg-green" icon="el-icon-circle-plus" @click="toModify('新增员工')">新增员工</el-button>-->
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" min-width="65" header-align="center" align="center" sortable />
            <el-table-column prop="name" label="用户名" min-width="90" header-align="center" align="center" />
            <el-table-column label="性别" min-width="70" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row.sex==''||scope.row.sex==null?'--':scope.row.sex }}
              </template>
            </el-table-column>
            <el-table-column prop="mobile" label="手机" min-width="120" header-align="center" align="center" >
              <template slot-scope="scope">
                {{ scope.row.mobile==''||scope.row.mobile==null?'--':scope.row.mobile }}
              </template>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" min-width="165" header-align="center" align="center" />
            <el-table-column prop="company_name" label="部门名称" min-width="100" header-align="center" align="center" />
            <el-table-column prop="hiredate" label="入职时间" min-width="110" header-align="center" align="center" sortable>
              <template slot-scope="scope">
                {{ scope.row.hiredate?parseTime(scope.row.hiredate, '{y}-{m}-{d}'):'--' }}
              </template>
            </el-table-column>
            <el-table-column prop="formal_time" label="转正时间" min-width="110" header-align="center" align="center" sortable>
              <template slot-scope="scope">
                {{ scope.row.formal_time?parseTime(scope.row.formal_time, '{y}-{m}-{d}'):'--' }}
              </template>
            </el-table-column>
            <el-table-column prop="annual_leave" label="年假天数" min-width="100" header-align="center" align="center"/>
            <el-table-column prop="last_login_time" label="最近登录时间" min-width="160" header-align="center" align="center" sortable />
            <el-table-column prop="job_status" label="在职状态" min-width="110" header-align="center" align="center" sortable >
              <template slot-scope="scope">
                {{ scope.row.job_status===1?'在职':'离职' }}
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="160" header-align="center" align="center" fixed="right">
              <template slot-scope="scope">
                <el-button type="primary" size="small" icon="el-icon-search" circle @click="toInfo('员工信息', scope.row)" />
                <el-button v-if="queryMatch(114)&&scope.row.id!==1" type="success" size="small" icon="el-icon-edit" circle class="bg-green" @click="toModify('编辑员工信息', scope.row)" />
                <!--<el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />-->
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, columnCheck, parseTime } from '../../../utils/table'
export default {
  data() {
    return {
      isShow: false,
      searchTerms: {
        company_id: '',
        searchValue: '',
        job_status: ''
      },

      companyList: [],

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 320,
      multipleSelection: [],
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {
    $route(to) {
      if (to.name === '员工列表') {
        this.getData()
        setTimeout(() => {
          this.isShow = true
        }, 500)
      } else {
        this.isShow = false
      }
    }
  },
  created() {
    if (this.$route.name === '员工列表') {
      this.isShow = true
      this.getData()
      this.getCompanyList()
    }
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, columnCheck, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'hr/getDataList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue,
        company_id: this.searchTerms.company_id,
        job_status: this.searchTerms.job_status
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取部门列表
     *  接口:'company/all-list'
     */
    getCompanyList() {
      const url = 'company/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.companyList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该员工信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'hr/del/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error('删除失败,请刷新或联系管理员!')
        })
      })
    },
    toInfo(title, row) {
      this.isShow = false
      const query = { title: title }
      row && (query.id = row.id)
      this.$router.push({
        path: '/personnel/staff/info',
        query: query
      })
    },
    toModify(title, row) {
      this.isShow = false
      const query = { title: title }
      row && (query.id = row.id)
      this.$router.push({
        path: '/personnel/staff/modify',
        query: query
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding: 10px 0;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
    }
    .el-select {
      padding-right: 15px;
      width: 180px;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}

.dialog {
  .content{
    text-align: center
  }
}

.el-tag {
  cursor: pointer;
}
</style>
